package com.school.device.dynamic;

import java.time.Instant;
import java.util.Calendar;
import java.util.Date;

import cn.hutool.core.util.IdUtil;
import com.mhuang.school.starter.minio.util.ToolUtil;
import com.school.device.config.DahuaInfo;
import com.school.device.util.SnowflakeIdeable;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.hutool.json.JSONObject;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class AlarmPushUtil {

	public static void pushMsg(DvIntellifHikvisonDevice device, String bigUrl, String smallUrl, String pushUrl) {
		if (ToolUtil.isEmpty(pushUrl))
			return;
		pushMsg(device.getDeviceType(), bigUrl, smallUrl, device.getId(), device.getDeviceName(), device.getDeviceAddress(), device.getGatewayNodeId(), pushUrl, device.getDeviceCode());
	}

	public static void pushMsg(DahuaInfo device, String bigUrl, String smallUrl, String pushUrl) {
		if (ToolUtil.isEmpty(pushUrl))
			return;
		pushMsg(device.getDeviceType(), bigUrl, smallUrl, device.getDeviceId(), device.getDeviceName(), device.getDeviceAddress(), device.getGatewayNodeId(), pushUrl, "");
	}

	public static void pushMsg(int type, String bigUrl, String smallUrl, String dvId, String dvName, String deviceAddress, Integer gatewayNodeId, String pushUrl, String deviceCode) {
		if (type == 1)
			sendMsg(bigUrl, smallUrl, 202, "高空抛物(大华)", dvId, dvName, deviceAddress, gatewayNodeId, pushUrl, deviceCode);
		if (type == 2)
			sendMsg(bigUrl, smallUrl, 201, "烟火检测(大华)", dvId, dvName, deviceAddress, gatewayNodeId, pushUrl, deviceCode);
		if (type == 3)
			sendMsg(bigUrl, smallUrl, 203, "人脸检测(大华)", dvId, dvName, deviceAddress, gatewayNodeId, pushUrl, deviceCode);
	}

	private static void sendMsg(String bigUrl, String smallUrl, int algorithmld, String algorithmName, String dvId, String dvName, String deviceAddress, Integer gatewayNodeId, String pushUrl, String deviceCode) {
		try {
			JSONObject bodyObject = new JSONObject();
			bodyObject.set("NotificationID",  IdUtil.simpleUUID());
			JSONObject paramJson = new JSONObject();
			paramJson.set("algorithmId", algorithmld);
			paramJson.set("algorithmName", algorithmName);
			Date from = Date.from(Instant.now());
			//Calendar calendar = Calendar.getInstance();
			//calendar.setTime(from); // 设置calendar的时间为当前时间
			//calendar.add(Calendar.SECOND, -30); // 减去30秒
			//Date time = calendar.getTime();
			paramJson.set("eventTime", from.getTime());
			paramJson.set("eventAddress", deviceAddress);
			paramJson.set("deviceId", dvId);
			if (gatewayNodeId != null)
				paramJson.set("gatewayNodeId", gatewayNodeId);
			paramJson.set("bigUrl", bigUrl);
			paramJson.set("smallUrl", smallUrl);
			paramJson.set("deviceName", dvName);
			paramJson.set("deviceCameraCode", deviceCode);
			bodyObject.set("AlarmEventDto", paramJson);
			String body = bodyObject.toString();
			log.info("【DH-ALARM推送请求】-->{}->{}", dvName, algorithmName);
			HttpResponse res = HttpRequest.post(pushUrl).body(body).execute();
			log.info("【DH-ALARM推送响应】-->{}", res.body());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
